<?php

namespace app\common\model\marketing;

use addons\xyb\XypHelper;
use think\Model;


class AssociationUsers extends Model
{

    // 表名
    protected $name = 'association_users';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    public static function getUsersEnrollStatus($user_id, $association_id){
        $response =  [
            'execute_days' => 0,
            'complete_days' => 0,
            'surplus_days' => 0,
            'deadline_date' => '',
            'obtained_point' => 0,
            'reward_point' => 0,
            'grant_status' => false,
            'enroll_status' => false,
            'enroll_date' => '',
        ];
        $details= self::where('association_id', $association_id)->where('user_id', $user_id)->order('id desc')->find();
        if(!$details){
            return $response;
        }

        $response['enroll_status'] = true;
        $response['enroll_date'] = date('Y-m-d', $details->createtime);
        $response['execute_days'] = (int)$details->execute_days;
        $response['complete_days'] = (int)$details->complete_days;
        $response['surplus_days'] =  (int)($response['execute_days'] - $response['complete_days']);
        $response['deadline_date'] = date('Y-m-d', $details->deadline);
        $response['obtained_point'] = (int)$details->obtained_point;
        $response['reward_point'] = (int)$details->reward_point;
        $response['grant_status'] =  ($details['grant_status'] == 2 || $response['surplus_date'] < 1);


    }


}